/* global $, jQuery */

$(document).ready(function(){
  $( '.link-pre' ).click(function(){
    if( $('.link-pre').hasClass('testo_esploso') ){
      $( '.testo_pre' ).css({
        'max-height' : '200px',
      });
      $(this).removeClass('testo_esploso');
      $(this).html('阅读更多');
    }else{
      $( '.testo_pre' ).css({
        'max-height' : '100%',
      });
      $(this).addClass('testo_esploso');
      $(this).html('关闭');
    }
  });

  $( '.link-post' ).click(function(){
    if( $('.link-post').hasClass('testo_esploso') ){
      $( '.testo_post' ).css({
        'max-height' : '200px',
      });
      $(this).removeClass('testo_esploso');
      $(this).html('阅读更多');
    }else{
      $( '.testo_post' ).css({
        'max-height' : '100%',
      });
      $(this).addClass('testo_esploso');
      $(this).html('关闭');
    }
  });

  var counter = 0;

  $('.launch-modal').click(function(){
    if( counter == 0 ){
      $('#informazioni-modal').modal('show');
    }

    counter++;
  });

  var formCatalog = $('#form-catalog');
  if (formCatalog.length) {
    var formInputCatalogoCode = formCatalog.find('input[name="catalogo"]');
    var formInputCatalogoType = formCatalog.find('input[name="catalogo-type"]');
    var formInputCatalogo = formCatalog.find('input[name="catalogo-name"]');
    // var formInputLingua = formCatalog.find('input[name="linguac"]');
  }

  $('.request-catalog').click(function() {
    formCatalog.removeClass('_catalogo-' + formInputCatalogoType.val());

    var cat = $(this).attr('data-catalog');
    var catname = $(this).attr('data-catalog-name');
    var cattype = $(this).attr('data-catalog-type');
    // var linguac = $(this).attr('data-linguac');

    formCatalog.addClass('_catalogo-' + cattype);

    formInputCatalogoCode.val(cat);
    formInputCatalogoType.val(cattype);
    formInputCatalogo.val(catname);
  });

  // var $form = $('.js-download-form');
  // if ($form.length > 0) {
  //   var nation = $form.find('select#Country');
  //   var previous;

  //   nation.on('change', function (event) {
  //     console.log(previous, this.value);
  //     if (previous) {
  //       $form.removeClass('_nazione-' + previous);
  //     }

  //     previous = this.value;

  //     $form.addClass('_nazione-' + previous);
  //   });
  // }

  $( window ).scroll(function(){
    if( $(document).scrollTop() > 0 ){
      $('.go-top').fadeIn(1000);
    }else{
      $('.go-top').fadeOut(1000);
    }
  });

  $('.go-top').click(function(){
    $('html, body').animate({
      scrollTop: 0
    }, 1000);
  });

  $(document).delegate('*[data-toggle="lightbox"]', 'click', function(event) { event.preventDefault(); $(this).ekkoLightbox(); });

  $('.scroll-to').click(function(){
    var target = $(this).attr('data-anchor-to');
    var goTo = $("#"+target).offset().top - 10;
    $('html, body').animate({
      scrollTop: goTo
    }, 1000);
  });

  setTimeout(function(){
    $('.loading').fadeOut();
  }, 3000);

  setTimeout(function(){
    $('.btn-fadeIn').fadeIn();
  }, 3500);

  var form = $("#newsletter-form");
  // form.find('input[type="button"]').click(function(event) {
  form.on('submit', function(event) {
    event.preventDefault();

    $(".newsletter__message").removeClass('.newsletter__message--show');

    if (form.find('#newsletter-mail').val() == "") {
      $(".newsletter__message").html("<span style=\"background:#e31e23\">Insert a valid mail</span>").addClass('newsletter__message--show');
    } else if (!form.find('#newsletter-privacy').is(":checked")) {
      $(".newsletter__message").html("<span style=\"background:#e31e23\">Accept our Privacy Policy to subscribe</span>").addClass('newsletter__message--show');
    } else {
      $.ajax({
        url: site_url + "/wp-content/themes/ortea/contact.php",
        type: 'POST',
        data: {
          pot: form.find('#pot').val(),
          lang: form.find('#lang').val(),
          mail: form.find('#newsletter-mail').val()
        },
        success: function(data) {
          $("#newsletter-mail").val("");
          $(".newsletter__message").html(data).addClass('newsletter__message--show');
        },
        error: function(jqXHR, textStatus) {
          $(".newsletter__message").html("<span style=\"background:#e31e23\">There was an error during your subscription</span>").addClass('newsletter__message--show');
        }
      });
    }
  });
});


